﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NguoiDung
{
    public partial class EditAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        

        public void LoadData()
        {
            if (Session["NguoiDung"] != null)
            {

                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                lbEmail.Text = user.Email;
                txtNgaySinh.Text = user.Birth.ToString("dd/MM/yyyy");
                txtTen.Text = user.UserName;
                txtDiaChi.Text = user.DiaChi;
                txtDiDong.Text = user.DiDong;
                txtDienThoai.Text = user.DienThoai;
                imgAvatar.ImageUrl = user.UrlAvatar;
                try
                {
                    var source=BLL.Admin.QuocGiaHelper.List_QuocGia();
                    droQuocGia.DataSource = source;
                    droQuocGia.DataTextField = "Ten";
                    droQuocGia.DataValueField = "QuocGiaId";
                    droQuocGia.DataBind();
                    droQuocGia.SelectedValue = user.tbl_DM_Tinh.QuocGiaId.ToString();
                    //


                    if (user.tbl_DM_Tinh.QuocGiaId != null)
                    {
                        var source1=BLL.Admin.TinhHelper.List_Tinh_Theo_QuocGia(user.tbl_DM_Tinh.QuocGiaId.Value);
                        droTinh.DataSource = source1;
                            
                        droTinh.DataTextField = "Ten";
                        droTinh.DataValueField = "TinhId";
                        droTinh.DataBind();
                        droTinh.SelectedValue = user.TinhId.ToString();
                    }

                }
                catch
                {
                }
            }
            else
            {
                Response.Redirect("/");
            }
        }

        protected void droQuocGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            droTinh.DataSource = BLL.Admin.TinhHelper.List_Tinh_Theo_QuocGia(int.Parse(droQuocGia.SelectedValue));
            droTinh.DataTextField = "Ten";
            droTinh.DataValueField = "TinhId";
            droTinh.DataBind();
        }
        private string UploadFile(FileUpload File)
        {
            //string Today = DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString();
            string today = DateTime.Now.ToShortDateString();
            string UploadPath = "/UserFile/NguoiDung/";
            string FilePath = UploadPath + DateTime.Now.ToShortDateString().Replace('/', '-') + DateTime.Now.ToLongTimeString().Replace(':', '-') + DateTime.Now.Millisecond + File.FileName;
            //string FilePath = UploadPath + File.FileName;

            /* N?u thu m?c t?n t?i */
            if (Directory.Exists(Server.MapPath(UploadPath)))
            {
                File.SaveAs(Server.MapPath(FilePath));
                //Utils.Help.CreateThumbnail(Server.MapPath(FilePath), 140, 105);
            }
            else
            {
                /* T?o thu m?c n?u chua t?n t?i */
                Directory.CreateDirectory(Server.MapPath(UploadPath));
                File.SaveAs(Server.MapPath(FilePath));
            }

            return FilePath;
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            string upfile = null;
            try
            {

                if (FileAvata.HasFile)
                {
                    if (FileAvata.PostedFile.ContentLength > 1049000)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe",
                                                            "alert('File ảnh phải nhỏ hơn 1Mb!');", true);
                        return;
                    }
                    upfile = UploadFile(FileAvata);
                }
                else
                {
                    upfile = null;
                }
                if (Session["NguoiDung"] != null)
                {

                    DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                    BLL.NguoiDung.NguoiDung.UpdateNguoiDung(user.UserId, txtTen.Text, Convert.ToBoolean(droGioiTinh.SelectedValue),
                                                            DateTime.Parse(txtNgaySinh.Text), droTTHN.SelectedItem.Text,
                                                            txtDienThoai.Text, txtDiDong.Text,
                                                            upfile, txtDiaChi.Text,
                                                            Convert.ToInt32(droTinh.SelectedValue));

                    pnSuaTaiKhoan.Visible = false;
                    pnThongBao.Visible = true;                    
                }
            }
            catch
            {

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/nguoi-dung/tai-khoan.html");
        }

        

    }
}